﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using System.Linq ;
using DevExpress.Utils;

namespace DevEx
{
    public partial class ucNguonVatTu : DevExpress.XtraEditors.XtraUserControl
    {
        public ucNguonVatTu()
        {
            InitializeComponent();
        }

        private void ucNguonVatTu_Load(object sender, EventArgs e)
        {
            KhoiTaodtgNguonVatTu("");
            KhoiTaoGiaoDien(true);
        }
       
        public void KhoiTaodtgNguonVatTu(String Sname)
        {
            //CNguonVatTu NguonVatTu = new CNguonVatTu();
            //dtgNguonVatTu.DataSource = NguonVatTu.SelectAllRecord();

            QLVTDataContext dc = new QLVTDataContext();
            var q = from p in dc.tblNguonVatTus select new { p.ID_NguonVatTu, p.NguonVatTu, p.KH_NguonVatTu };
            dtgNguonVatTu.DataSource = q;

            gridView1.Columns.ColumnByFieldName("ID_NguonVatTu").Visible = false;
            
            //focus
            int focusrow = 0;
            if (Sname != "")
            {
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    // if (dtcustomer.Rows[i]["CUST_NAME"].ToString().ToUpper() == Sname.ToUpper())
                    if (gridView1.GetRowCellValue(i, "NguonVatTu").ToString().ToUpper() == Sname.ToUpper())
                    {
                        focusrow = i;
                    }
                }
            }         

            gridView1.FocusedRowHandle = focusrow;
            FormatdtgNguonVatTu();
        }

        public void FormatdtgNguonVatTu()
        {
            var cl_NVT = gridView1.Columns.ColumnByFieldName("NguonVatTu");
            cl_NVT.Width = 150;
            cl_NVT.Caption = "Nguồn vật tư";
            cl_NVT.OptionsColumn.AllowEdit = false;
            cl_NVT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_NVT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;

            var cl_KHNVT = gridView1.Columns.ColumnByFieldName("KH_NguonVatTu");
            cl_KHNVT.Width = 150;
            cl_KHNVT.Caption = "Ký hiệu";
            cl_KHNVT.OptionsColumn.AllowEdit = false;
            cl_KHNVT.AppearanceCell.TextOptions.HAlignment = HorzAlignment.Near;
            cl_KHNVT.AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;            
        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (Convert.ToInt32(e.RowHandle) + 1).ToString();
                //if (!indicatorIcon)
                e.Info.ImageIndex = -1;
            }
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtNguonVatTu.EditValue = gridView1.GetFocusedRowCellValue("NguonVatTu");
            txtKyHieu.EditValue = gridView1.GetFocusedRowCellValue("KH_NguonVatTu");            
        }


        Boolean fThem = false;
        Boolean fSua = false;
        CNguonVatTu m_NguonVatTu = new CNguonVatTu();

        private void btnThem_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtNguonVatTu.EditValue = "";
            txtKyHieu.EditValue = "";
            txtNguonVatTu.Focus();
            fThem = true;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(false);
            txtNguonVatTu.Focus();
            fSua = true;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Chú ý", "Bạn muốn xóa NguonVatTu này", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                int ID_NguonVatTu = 0;
                ID_NguonVatTu = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_NguonVatTu"));
                m_NguonVatTu.Delete(ID_NguonVatTu);                
                KhoiTaodtgNguonVatTu("");
            }
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            if (fThem)
            {
                tblNguonVatTu NguonVatTu = new tblNguonVatTu();
                NguonVatTu.NguonVatTu = txtNguonVatTu.Text;
                NguonVatTu.KH_NguonVatTu = txtKyHieu.Text;
                m_NguonVatTu.Insert(NguonVatTu);
            }

            if (fSua)
            {
                tblNguonVatTu NguonVatTu = new tblNguonVatTu();
                NguonVatTu.ID_NguonVatTu = Convert.ToInt32(gridView1.GetFocusedRowCellValue("ID_NguonVatTu"));
                NguonVatTu.NguonVatTu = txtNguonVatTu.Text;
                NguonVatTu.KH_NguonVatTu = txtKyHieu.Text;
                m_NguonVatTu.Update(NguonVatTu);
            }

            KhoiTaoGiaoDien(true);
            KhoiTaodtgNguonVatTu(txtNguonVatTu.Text);
            fThem = false;
            fSua = false;

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien(true);
            fThem = false;
            fSua = false;
        }

        public void KhoiTaoGiaoDien(Boolean f)
        {
            btnThem.Enabled = f;
            btnSua.Enabled = f;
            btnXoa.Enabled = f;
            btnGhi.Enabled = !f;
            btnHuy.Enabled = !f;
            txtNguonVatTu.Properties.ReadOnly = f;
            txtKyHieu.Properties.ReadOnly = f;
        }

      
        
    }
}
